import axios from "axios";
import {
    Modal
} from 'antd';
export default class Axios {

    //封装表格动态获取数据的方法
    static ajax(options) {

        // 添加loading效果
        if (options.data && options.data.isShowLoading !== false) {
            let loading = document.getElementById('ajaxLoading');
            loading.style.display = 'block';
        }
        //基础配置
        let baseUrl = 'https://www.fastmock.site/mock/b187e7691277665a9024766157b6aac3/imooc';
        return new Promise((resolve, reject) => {
            axios({
                url: options.url,
                method: 'get',
                baseURL: baseUrl,
                timeout: 5000,
                params: (options.data && options.data.params) || ''
            }).then((res) => {
                if (options.data && options.data.isShowLoading !== false) {
                    let loading = document.getElementById('ajaxLoading');
                    loading.style.display = 'none';
                }
                if (res.status === 200) {
                    if (res.data.code === '200') {
                        resolve(res);
                    } else {
                        Modal.info({
                            title: '提示',
                            content: res.data.msg
                        });
                    }
                } else {
                    reject(res.data);
                }
            })
        });
    }
    //请求列表的封装
    static requestList(_this,url, params) {
        var data = {
            params: params
        }
        this.ajax({
            url,
            data
        }).then((res) => {
            if (res) {
                let list = res.data.result.item_list.map((item, index) => {
                    item.key = index;
                    return item;
                });
                _this.setState({
                    list: list
                })
            }
        })
    }

}